package org.aperture.eve.data;

import java.net.MalformedURLException;
import java.net.*;
import java.io.*;
import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;

import javax.xml.parsers.*;
//import javax.xml.transform.*;
//import javax.xml.transform.dom.*;
//import javax.xml.transform.stream.*;
import org.w3c.dom.*;

import java.nio.file.*;

public class Get_EVE_Conquerable_Station_List {

	public static void main(String[] args) throws Exception {
		new Get_EVE_Conquerable_Station_List().getSkills();
	}
	
	private void getSkills() {
		try {
			URL skillTree = new URL("https://api.eveonline.com/eve/ConquerableStationList.xml.aspx");
			URLConnection connection = skillTree.openConnection();
//			print_https_cert(secureConnection);
			writeSkillTreeToFile(connection.getInputStream());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	private void writeSkillTreeToFile(InputStream input) {
		InputStream inputStream = null;
		OutputStreamWriter outputStream = null;
		Path outputFile = Paths.get("D:/javatest/conquerable_station_list.xml");

//		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
//		Document document = documentBuilder.newDocument();
		
		try {
//			inputStream = new BufferedReader(new InputStreamReader(input));
//			FileOutputStream fileOut = new FileOutputStream("D:/javatest/test.xml");
//			outputStream = new OutputStreamWriter(fileOut, "UTF8");
			DataInputStream dataInputStream = new DataInputStream(input);

			Files.createFile(outputFile);
			
			byte[] buffer = new byte[1024];
			try {

				int numRead;
				int test_download = 0;
				do {
//					System.out.println("here.1.");
					numRead = dataInputStream.read(buffer);
					if (numRead > 0) {
						System.out.print(buffer);
						Files.write(outputFile, buffer, StandardOpenOption.APPEND);
					}
					if (test_download <= 0) {
						for (int i = 0; i < 1024; i++) {
							System.out.println(buffer[i]);
						}
					}
					test_download++;
				} while (numRead != -1);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();	
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
			
			
//			while ((buffer = dataInputStream.read()) != null)
//				Files.write(outputFile, buffer);
			
//			String inputLine;
//			while ((inputLine = inputStream.readLine()) != null)
//				outputStream.write(inputLine);
			
//			byte[] buffer = new byte[1024];
//			try {
//				int numRead;
//				do {
//					numRead = inputStream.read();
//					if (numRead > 0) {
//						outputStream.write(buffer);
//					}
//				}
//			}
			
			
			
//			try {
//				int numRead;
//				do {
//					numRead = fileInput.read(buffer);
//					if (numRead > 0) {
//						md.update(buffer, 0, numRead);					
//					}
//				} while (numRead != -1);
//				fileInput.close();
//			} catch(Exception e) {
//				e.printStackTrace();
//			} finally {
//	            if (fileInput != null) {
//	                fileInput.close();
//	            }
//			}
			
			
			
			
			
			
			
			
//		} catch (IOException e) {
//			e.printStackTrace();
//		} 
	      catch (IOException e) {
	    	  e.printStackTrace();
	      }
		  finally {
			if (inputStream != null) {
				try {
					inputStream.close();	
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
				outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}}
	
		
		

//		//URLConnection connection = skillTree.openConnection();
//		
//		BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//		String inputLine;
//		while ((inputLine = in.readLine()) != null)
//			System.out.println(inputLine);
//		in.close();
		
	
		
	private void print_https_cert(HttpsURLConnection con){
			   
		if(con!=null){
			 
			try {
			 
				System.out.println("Response Code : " + con.getResponseCode());
				System.out.println("Cipher Suite : " + con.getCipherSuite());
				System.out.println("\n");
			 
				Certificate[] certs = con.getServerCertificates();
				for(Certificate cert : certs){
				   System.out.println("Cert Type : " + cert.getType());
				   System.out.println("Cert Hash Code : " + cert.hashCode());
				   System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
				   System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
				   System.out.println("\n");
			}
			 
				} catch (SSLPeerUnverifiedException e) {
					e.printStackTrace();
				} catch (IOException e){
					e.printStackTrace();
				}
			 
			     }
			 
			   }
		
}
